local Screen = require "widgets/screen"
local Widget = require "widgets/widget"
local ImageButton = require "widgets/imagebutton"
local TEMPLATES = require "widgets/redux/templates"
local Image = require "widgets/image"
local Text = require "widgets/text"
local t_util = require "libs/tableutil"
local s_util = require "libs/settingmanager"
local s_data = s_util:GetSettingLine("screendata")

-- id，说明文字，提示文字，回调函数

local ScreenScreen = Class(Screen, function(self, data)
    Screen._ctor(self, "ScreenScreen")

    local black = self:AddChild(ImageButton("images/global.xml", "square.tex"))
    black:SetOnClick(function() TheFrontEnd:PopScreen() end)
    local bg = black.image
    bg:SetVAnchor(ANCHOR_MIDDLE)       -- 默认在屏幕左下角
    bg:SetHAnchor(ANCHOR_MIDDLE)
    bg:SetScaleMode(SCALEMODE_FILLSCREEN)
    bg:SetTint(0,0,0,0)    -- 设置透明

    local book = self:AddChild(Widget("root"))
    self.book = book
    book:SetVAnchor(ANCHOR_MIDDLE)
    book:SetHAnchor(ANCHOR_MIDDLE)
    self.w,self.h = 1000, 625
    book.bg = book:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_menu_bg.tex"))
    book.bg:SetSize(self.w, self.h)
    book.title = book:AddChild(Text(HEADERFONT, 45, "画质渲染", UICOLOURS.BLACK))
    book.title:SetPosition(0, self.h/2-45)
    book.title_line = book.title:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_line_break.tex"))
    book.title_line:SetPosition(0, -40)
    book.bar = book:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_scroll_bar.tex"))
    book.bar:SetSize(5, self.h-240)
    book.bar:SetPosition(0, -40)

    self.data = data

    self.arr_l = book:AddChild(ImageButton("images/frontend.xml", "turnarrow_icon.tex", "turnarrow_icon_over.tex", nil, nil, nil, {1,1}, {0,0}))
    self.arr_r = book:AddChild(ImageButton("images/frontend.xml", "turnarrow_icon.tex", "turnarrow_icon_over.tex", nil, nil, nil, {1,1}, {0,0}))
    local arrow_size = self.arr_l:GetSize()
    local arrow_scale = 60/arrow_size
    self.arr_r:SetNormalScale(arrow_scale)
    self.arr_r:SetFocusScale(arrow_scale*1.2)
    self.arr_l:SetNormalScale(arrow_scale)
    self.arr_l:SetFocusScale(arrow_scale*1.2)
    self.arr_l:SetPosition(-self.w/6, self.h/2-45)
    self.arr_l:SetScale(1, .5, 1)
    self.arr_r:SetPosition(self.w/6, self.h/2-45)
    self.arr_r:SetScale(-1, .5, 1)
    self.arr_r:SetHoverText("下一页")
    self.arr_l:SetHoverText("上一页")
    self.arr_l:SetOnClick(function() self.page = self.page - 1 self:Paint() end)
    self.arr_r:SetOnClick(function() self.page = self.page + 1 self:Paint() end)

    local data_count = #data
    self.num = 10
    self.page_max = math.ceil(data_count/self.num)
    self.page = 1
    self:Paint()
end)

function ScreenScreen:Paint()
    self.arr_l:Show()
    self.arr_r:Show()
    local page = self.page
    if page <= 1 then
        self.arr_l:Hide()
    end
    if page >= self.page_max then
        self.arr_r:Hide()
    end    
    
    if self.lines then self.lines:Kill() end
    self.lines = self.book:AddChild(Widget("lines"))
    self.lines:SetPosition(-self.w/2+120, self.h/2-200)

    for i=1,self.num do
        local nodot = (self.page-1)*self.num+i
        local b_data = self.data[nodot]
        if not b_data then break end
        local btn = self.lines:AddChild(ImageButton("images/quagmire_recipebook.xml", "cookbook_known.tex"))
        local btn_size = btn:GetSize()
        local btn_scale = 60/btn_size
        btn:SetNormalScale(btn_scale)
        btn:SetFocusScale(btn_scale*1.2)
        btn.value = s_data[b_data.id]
        btn.img_or = btn:AddChild(Image("images/shroomcake_moreicons.xml", btn.value and "right.tex" or "wrong.tex"))
        btn.img_or:SetSize(45, 45)
        btn:SetPosition((i-1)%2 * (self.w/2-60), -math.floor((i-1)/2)*70)
        btn:SetOnClick(function()
            btn.value = not btn.value
            btn.img_or:SetTexture("images/shroomcake_moreicons.xml", btn.value and "right.tex" or "wrong.tex")
            btn.img_or:SetSize(45, 45)
            b_data.fn(btn.value)
            s_data[b_data.id] = btn.value
            s_util:SaveSettingLine("screendata", s_data)
        end)
        btn.labeltext = btn:AddChild(Text(HEADERFONT, 40, b_data.label, UICOLOURS.BLACK))
        btn.labeltext:SetPosition(150, 0)
        btn:SetHoverText(b_data.hover, {font = NEWFONT_OUTLINE, offset_y = 90 })
    end
end


function ScreenScreen:OnControl(control, down)
    if ScreenScreen._base.OnControl(self, control, down) then return true end

    if not down and (control == CONTROL_MAP or control == CONTROL_CANCEL) then
		TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/click_move")
        TheFrontEnd:PopScreen()
        return true
    end
end

return ScreenScreen